package com.room.service;

import com.room.mapper.RoleMapper;
import com.room.mapper.StudentMapper;
import com.room.mapper.UserMapper;
import com.room.pojo.NodisClass;
import com.room.pojo.Student;
import com.room.pojo.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @创建人 lin
 * @创建时间 2019/10/7
 * @描述
 */
@Service
public class StudentService {
    @Autowired
    StudentMapper studentMapper;

    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;

   public List<Student> find(Map map){
     return   studentMapper.find(map);
    }

    public  int update(Student student){
        return studentMapper.update(student);
    }

    //获取未分配宿舍的学生总数和男女人数
    public List<NodisClass> findByNodis(String collegeId,String grade){
       return studentMapper.findByNodis(collegeId,grade);
    }

    //根据分页获取学生数据
    public List<Student> getStudentByPage(Integer page,Integer size){
       int start  = (page-1)* size;
       return studentMapper.getStudentByPage(start, size);
    }

    //获取学生的人数
    public Long getStudentCount(){
       return studentMapper.getStudnetCount();
    }

    //根据传进的id删除学生数据
    public boolean deleteStuById(String ids){
        String[] split = ids.split(",");
        for (int i = 0;i<split.length;i++){
          String studentNo =   studentMapper.getStuNoById(split[i]);
          if(studentNo!=null){
              //删除用户表和角色表对应的学生的数据
             long uid =  userMapper.getUserIdByUserName(studentNo);
              roleMapper.deleteRoleByUid(uid);
              userMapper.deleteUserByUserName(studentNo);
          }
        }
        return studentMapper.deleteStuById(split) == split.length;
    }

    //获取所有学生的数据，导入导出的时候会用到
    public List<Student> getAllStudent(){
       return studentMapper.getAllStudent();
    }

    public int addStus(List<Student> stus){
       return studentMapper.addStus(stus);
    }

    //获取所有导出到excel的学生数据
    public  List<StudentVO> getAllStudentVO(){
       return studentMapper.getAllStudentVO();
    }

    //根据学院id获取学生数据
    public List<StudentVO> getAllStudentVOByCollegeId(String collegeId){
       return studentMapper.getAllStudentVOByCollegeId(collegeId);
    }

    //根据学号修改学生的个人数据
    public int updateStudentBystuNo(Student student){
       return studentMapper.updateStudentBystuNo(student);
    }

    //添加学生
    public int insert (Student student){
      return studentMapper.insert(student);
    }

    //获取最大学号
    public String getMaxId(){
       return studentMapper.getMaxId();
    }

 //根据学号查询id
    public String getStuNoById(String id){
       return studentMapper.getStuNoById(id);
    }

    //根据学号获取学生
    public Student getStudentByNo(String studentNo){
       return studentMapper.getStudentByNo(studentNo);
    }

    //查询指定学院的学生数据
    public List<Student> getStudenrByCollegeName(String collegeId){
       return studentMapper.getStudenrByCollegeName(collegeId);
    }

    //查询某学院学生的人数
    public long getTotal(String collegeId){
       return studentMapper.getTotal(collegeId);
    }
}
